<%@page import="org.apache.commons.lang3.StringUtils"%>
<%@page import="com.kerrykidz.sales.dao.model.PosOrderEntity"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib uri="token-tag" prefix="t"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<jsp:include page="/common/commonCss.jsp"></jsp:include>
<jsp:include page="/common/commonJs.jsp"></jsp:include>
<script type="text/javascript"
	src="${basePath }commonJs/jquery.imageScroller.js"></script>
<script type="text/javascript"
	src="${basePath }commonJs/quinn_commons.js"></script>


<script type="text/javascript"
	src="${basePath }resources/membership/finger.js"></script>

<script type="text/javascript">
	$(document).ready(function() {
		var errMsg = '${errMsg}';
		if (errMsg.length > 0) {
			alertInfo(errMsg);
		}

		$("#listBox").imageScroller({
			next : "btnNext",
			prev : "btnPrev",
			frame : "list",
			child : "li",
			auto : false
		});
	});

	var memId = null;

	var path = null;

	var money = null;

	function selectedme(o, id, name, p, m) {
		$(".checkedStyle").removeClass("checkedStyle");
		$(o).addClass("checkedStyle");
		alertInfo("已选择" + name + "为支付账户");
		memId = id;
		path = p;
		money = m;
	}

	function okpay() {
		if (path == null) {
			alertInfo("请选择一个小朋友");
			return;
		}
		$("#img_mem").attr("src", path);
		$('#e').window('close');
		$("#czxf").attr("checked", true);
		$("#blance").html(money);
		$("#czxf_ss").val(zyjfe);
		$("#payer").val(memId);
	}

	function openWindow() {
		var zw = collectFingerFeatrue();
		$
				.ajax({
					url : "${basePath}payment/findCusMemByFinger.shtml",
					type : "post",
					data : {
						finger : zw
					},
					dataType : "json",
					async : false,
					success : function(d) {
						$("#dl_1 dd").remove();
						if (d == null || d.length == 0) {
							alertInfo("未找到合适的人员");
						} else {
							var data = d[1];
							if (data.length == 1) {
								var o = data[0];
								var div = getLiHtml(o);
								$("#liA").html(div);
							} else if (data.length == 2) {
								var o = data[0];
								var o2 = data[1];
								var div1 = getLiHtml(o);
								var div2 = getLiHtml(o2);
								$("#liA").html(div1);
								$("#liB").html(div2);
							} else if (data.length > 2) {
								$("ul[id='list']").find("li").remove();
								$.each(data, function(ind, obj) {
									var div = getLiHtml(obj);
									var li = "<li>" + div + "</li>";
									$("ul[id='list']").append(li);
								});
							}
							$
									.each(
											data,
											function(ind, obj) {
												var yfje = $("#yfje").html();
												var dd = "<dd>";
												dd += "<ul class=\"ul_radio\">";
												dd += "<li>"
														+ formatName(obj.memName)
														+ "</li>";
												var b = (obj.balance == null ? 0
														: obj.balance);
												dd += "<li>余额：<span class=\"color_lv mlr5\">￥<span id=\"mem_b_"+obj.pkMemId+"\">"
														+ b
														+ "</span></span></li>";
												dd += "<li>实收：￥<input type=\"text\" value=\""+yfje+"\" class=\"w70 f14 iput_t\" name=\"hyss\" id=\""+obj.pkMemId+"_"+d[0]+"\"></li>";
												dd += "</ul> </dd>";
												$("#dl_1").append(dd);
											});

							$("#czxf").attr("checked", true);
						}
					}
				});
	}

	function formatName(memname) {
		if (memname.length > 3) {
			return memname = memname.substring(0, 3);
		}
		return memname;
	}

	function getLiHtml(o) {
		var div = "<div class=\"fl shouyin_hui m10\">";
		div += "<p>";
		div += "<img width=\"102\" height=\"136\" src=\"${basePath}common/showImage.shtml?imagePath="
				+ o.imagePath + "\">";
		div += "</p>";
		div += "<p>";
		div += formatName(o.memName) + "&nbsp;&nbsp;<span>" + o.age
				+ "岁</span>";
		div += "</p>";
		div += "<p class=\"f11\">";
		div += o.isVip == "1" ? "会员" : "非会员";
		div += "</p>";
		div += "<p>" + o.pkMemId + "</p>";
		div += " </div>";
		return div;
	}
	
	function showMoney(o,m){
		var c = $(o).attr("checked")=='checked';
		var v = $(o).val();
		if(c){
			$("#fczxf").attr("checked",true);
			$("#"+v).val(m);
		}else{
			$("#"+v).val(null);
		}
	}
</script>


<style>
#listBox {
	width: 160px;
	height: 230px !important;
}

#container {
	width: 800px;
	position: absolute;
}

.thistab_a input {
	background: #ccc !important;
	color: #333 !important;
}

.checkedStyle {
	border-color: blue;
}
</style>


</head>
<body class="bg">

	<div class="content m_auto">
		<div class="content_l mt10">
			<ul>
					<shiro:hasPermission name="payment/skmx.shtml">
					<li class="content_hover"><a
						href="${basePath }payment/skmx.shtml"><i class="nav_icon_k"></i><br>收款</a></li>
				</shiro:hasPermission>
				<shiro:hasPermission name="payment/symx.shtml">
					<li><a href="${basePath }payment/symx.shtml"><i
							class="nav_icon_l"></i><br>收银明细</a></li>
				</shiro:hasPermission>
			</ul>
		</div>
		<div class="content_r mt10">
			<div class="p20">
				<h1 class="f16">订单信息</h1>
				<div class="goods_order_top fixed mt10 f14">
					<ul class="ml10">
						<li><label>订单号：</label>${order.pkOrderId }</li>
						<li><label>当前操作人：</label>${loginUser.name }</li>
						<li>${loginUser.branName }</li>
					</ul>
				</div>
				<table class="table mt10">
					<tr>
						<th>序号</th>
						<th>商品编号</th>
						<th>商品名称</th>
						<th>数量</th>
						<th>单价</th>
						<th>小计</th>
						<th>折扣率</th>
						<th>备注</th>
						<th>折后金额</th>
					</tr>

					<c:forEach var="p" items="${products }" varStatus="vs">
						<tr>
							<td>${vs.index+1 }</td>
							<td>${p.fkPrdId }</td>
							<td><c:choose>
									<c:when test="${p.type eq '1' }">
									${p.productName }
								</c:when>
									<c:otherwise>
									充值
								</c:otherwise>
								</c:choose></td>
							<c:choose>
								<c:when test="${p.type eq '1' }">
									<td>${p.count }</td>
									<td>${p.price }</td>
									<td>${p.price*p.count }</td>
								</c:when>
								<c:otherwise>
									<td>-</td>
									<td>-</td>
									<td>${p.count }</td>
								</c:otherwise>
							</c:choose>
							<td>${empty p.disValue ? '无' :p.disValue  }</td>
							<td>${p.discComments }</td>
							<td>${p.disPrice }</td>
						</tr>
					</c:forEach>

					<tr>
						<td colspan="10">
							<ul class="goods_ul fr p10">
								<li>应退金额<span class="fb mlr5 color_org f18">￥<span
											id="yfje">${ytje}</span></span></li>
							</ul>
						</td>
					</tr>
				</table>

				<c:if test="${order.feteFlag eq '1' || order.giftNo !=null }">
					<div class="mt10">
						<table class="table">
							<tr>
								<th>宴请单</th>
								<th>礼券</th>
							</tr>
							<tr>
								<td><input type="button" value="查看附件" class="btn_l"
									onclick="window.location.href='${basePath}common/showImage.shtml?imagePath=${order.fkFeteFile }'"></td>
								<td><input type="button" value="查看" class="btn_l"
									onclick="$('#exx').window('open')"></td>
							</tr>
						</table>
					</div>
				</c:if>
				<textarea style="width: 1116px; margin-top: 10px;" class="textare"
					disabled placeholder="客户要求"></textarea>
				<div class="chuzhi mt10 p10">
					<a href="#" onclick="$('#e').window('open')"><img
						src="${basePath}commonCss/images/zhiwen_w.png" class="fl p10"></a>
					<div id="btnNext" class="ml20">
						<a href="javascript:void(0);"
							class="collection_t_l collection_t_lx"></a>
					</div>
					<div id="listBox">
						<ul id="list">
							<c:if test="${empty yue }">
								<li>
									<!----扫描前---->
									<div class="fl shouyin_hui m10">
										<img src="${basePath}commonCss/images/index_02.png"
											width="111" height="186">
									</div> <!----扫描前---->
								</li>
							</c:if>
							<c:if test="${not empty yue }">
								<c:forEach var="p" items="${yue }">
									<li>
										<!----扫描后---->
										<div class="fl shouyin_hui m10">
											<p>
												<img width="102" height="136"
													src="${basePath}common/getMemTitle.shtml?memId=${p.fkAccount}">
											</p>
											<p>${p.accountName }</p>
										</div> <!----扫描后---->
									</li>
								</c:forEach>
							</c:if>

						</ul>
					</div>
					<div id="btnPrev">
						<a href="javascript:void(0);"
							class="collection_t_r collection_t_rx"></a>
					</div>


					<c:if test="${empty yue }">
						<!----------不可使用余额---->
						<dl class="shouyin_shou border_r">
							<h2 class="f16" style="color: #ccc;">
								<input type="checkbox" class="mr5" id="czxf">储值消费
							</h2>
							<span class="shouyin_no">不可退款到余额</span>
						</dl>
						<!----------不可使用余额---->
					</c:if>

					<c:if test="${not empty yue }">
						<!----------可使用余额---->
						<dl class="shouyin_shou border_r">
							<h2 class="f16">
								<input type="checkbox" class="mr5" id="czxf">储值消费
							</h2>
							<c:forEach var="y" items="${yue }">
								<dd>
									<ul class="ul_radio ">
										<li>${y.accountName }</li>
										<li>支付：<span class="color_lv mlr5 ">￥${y.amount }</span></li>
										<li>实退：￥<input type="text" value=""
											class="w70 f14 iput_t" id="${y.fkAccount }" name="accPay"
											onchange="changeAccAmount(this,${y.amount })"></li>
									</ul>
								</dd>
							</c:forEach>


						</dl>
					</c:if>
					<!----------可使用余额---->

					<dl class="shouyin_shou pl50">

						<c:choose>
							<c:when test="${xianjin ==null and yinhangka == null }">
								<h2 class="f16" style="color: #ccc;">
									<input type="checkbox" class="mr5" disabled="disabled"
										id="fczxf" >非储值消费
								</h2>
							</c:when>
							<c:otherwise>
								<h2 class="f16">
									<input type="checkbox" class="mr5" id="fczxf">非储值消费
								</h2>
							</c:otherwise>
						</c:choose>
						<c:if test="${xianjin==null }">
							<dd>
								<input type="radio" class="mr5" disabled="disabled" value="xjst"
									name="fczxf_r">现金支付￥0
							</dd>
							<dd class="ml20">
								实退：￥<input type="text" value="" class=" w70 f14 iput_t"
									disabled="disabled">
							</dd>
						</c:if>
						<c:if test="${xianjin!=null }">
							<dd>
								<input type="radio" class="mr5" name="fczxf_r" value="xjst" onclick="showMoney(this,${vir ? xianjin:xianjin.amount })">现金支付￥${vir ? xianjin:xianjin.amount }
							</dd>
							<dd class="ml20">
								实退：￥<input type="text" id="xjst" value=""
									class=" w70 f14 iput_t">
							</dd>
						</c:if>
						<c:if test="${yinhangka==null }">
							<dd>
								<input type="radio" class="mr5" disabled="disabled"
									name="fczxf_r">银行卡支付￥0
							</dd>
							<dd class="ml20">
								实退：￥<input type="text" value="" class=" w70 f14 iput_t"
									disabled="disabled">
							</dd>
						</c:if>
						<c:if test="${yinhangka!=null }">
							<dd>
								<input type="radio" class="mr5" name="fczxf_r" value="yhkst" onclick="showMoney(this,${vir ? yinhangka:yinhangka.amount })">银行卡支付￥${vir ? yinhangka:yinhangka.amount }
							</dd>
							<dd class="ml20">
								实退：￥<input type="text" id="yhkst" value=""
									class=" w70 f14 iput_t">
							</dd>
						</c:if>
					</dl>
				</div>




				<div class="btm_btn mt10">
					<dl>
						<dd>
							<input type="button" class="btn" value="确认退款" onclick="refund();">
						</dd>
						<dd>
							<input type="button" class="btn" value="取消"
								onclick="cancelOrder();">
						</dd>
					</dl>
				</div>
				
				<!---弹出层--->
		<div id="exx" class="easyui-window" title="查看礼券"
			data-options="modal:true,closed:true,iconCls:'icon-save'"
			style="width: 500px; height: 350px; position: relative;">
			<div class="m10">
				<table class="table">
					<tr>
						<th>礼券号</th>
					</tr>
					<%
						PosOrderEntity order =  (PosOrderEntity)request.getAttribute("order");
												if(StringUtils.isNotBlank(order.getGiftNo())){
													String giftno[] = order.getGiftNo().split(",");
													for(String gn : giftno){
					%>
					<tr>
						<td><%=gn%></td>
					</tr>
					<%
						}
												}
					%>
				</table>
			</div>

				<form action="${basePath }payment/refundMoney.shtml" method="post"
					id="payform">
					<t:token />
					<input type="hidden" name="orderId" value="${order.pkOrderId }">
					<input type="hidden" name="yue" id="yue"> <input
						type="hidden" name="xj" id="xj"> <input type="hidden"
						name="yhk" id="yhk"> <input type="hidden" name="isPrepay"
						value="${isPrepay }">
				</form>
</body>
</html>


<script type="text/javascript">
	function refund() {


		var ye_amount = [];
		var xj_amount = 0;
		var yhk_amount = 0;
		var czxf = $("#czxf").attr("checked") == "checked";
		var fczxf = $("#fczxf").attr("checked") == "checked";
		
		if (czxf) {
			$("input:text[name='accPay']").each(function() {
				var v = $(this).val();
				if (isNaN(v)) {
					alertInfo("输入金额只能为数字");
					return;
				} else if(parseFloat(v)>0){
					var o = v+"_"+$(this).attr("id");
					ye_amount.push(o);
				}
			});
		}

		if (fczxf) {
			var k = $("input:radio[name='fczxf_r']:checked").val();
			if(k!=null&&k.length>0){
				var v = $("#" + k).val();
				if (isNaN(v)) {
					alertInfo("输入金额只能为数字");
					return;
				}
				if (k == "fczxf_yhk") {
					yhk_amount = parseInt(v);
				} else {
					xj_amount = parseInt(v);
				}
			}
		}
		
		
		if(ye_amount.length==0&&xj_amount==0&&yhk_amount==0){
			alertInfo("请至少选择一个要退款的方式!");
			return;
		}
		


		$.messager.confirm('info', "确认要退款吗?", function(b) {
			if (b) {
				$("#yue").val(ye_amount.toString());
				$("#xj").val(xj_amount);
				$("#yhk").val(yhk_amount);
				$("#payform")[0].submit();
			}
		});
	}

	function cancelOrder() {
		$.messager.confirm(
						"提示",
						"确认取消该订单吗",
						function(d) {
							if (d) {
								window.location.href = "${basePath}order/cancelOrder.shtml?id=${order.pkOrderId}"
							}
						});
	}
	
	function changeAccAmount(o,a){
		var ov = $(o).val();
		if(parseFloat(ov)>a){
			alertInfo("退款金额不能超过支付金额");
			$(o).val(0);
			return;
		}
	}
</script>